<ui:composition xmlns="http://www.w3.org/1999/xhtml"
	xmlns:ui="http://java.sun.com/jsf/facelets"
	xmlns:f="http://java.sun.com/jsf/core"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:c="http://java.sun.com/jsp/jstl/core"
	xmlns:p="http://primefaces.org/ui"
	template="/templates/masterLayout.xhtml">

	<ui:define name="pageTitle">#{out.partners_title}</ui:define>
	<ui:define name="header"> -> #{out.partners_title}</ui:define>

	<ui:define name="head-extra">
		<!-- custom css -->
	</ui:define>

	<ui:define name="content">

		<h:form id="partnerForm">
			<p:growl id="msgs" for="msgs" showDetail="true" autoUpdate="true" />
			<p:dataTable var="currentPartner" value="#{partnerBean.partnerModel}"
				selectionMode="single"
				selection="#{partnerController.selectedPartner}"
				rowKey="#{currentPartner.id}" paginator="true" rows="10"
				paginatorTemplate="{RowsPerPageDropdown} {FirstPageLink} {PreviousPageLink} {CurrentPageReport} {NextPageLink} {LastPageLink}"
				rowsPerPageTemplate="5,10,15" id="partnerTable" lazy="true"
                                emptyMessage="#{out.empty_message}">
				<p:ajax event="rowSelect"
					listener="#{partnerController.onRowSelect}"
					update=":partnerForm:msgs,:partnerCommandsForm,:editPartnerDialog,:showOrdersDialog" />
				<p:ajax event="rowUnselect"
					listener="#{partnerController.onRowUnselect}"
					update=":partnerForm:msgs,:partnerCommandsForm,:showOrdersDialog" />
				<p:column headerText="#{out.partners_table_name}"
					sortBy="#{currentPartner.name}" filterBy="#{currentPartner.name}">
					<h:outputText value="#{currentPartner.name}" />
				</p:column>
				<p:column headerText="#{out.partners_table_address}"
					sortBy="#{currentPartner.address}"
					filterBy="#{currentPartner.address}">
					<h:outputText value="#{currentPartner.address}" />
				</p:column>
				<p:column headerText="#{out.partners_table_type}"
					sortBy="#{currentPartner.type}" filterBy="#{currentPartner.type}">
					<h:outputText value="#{currentPartner.type}" />
				</p:column>
			</p:dataTable>
		</h:form>

		<p:dialog id="editPartnerDialog" header="#{out.partners_edit_partner}"
			widgetVar="dlg_edit" modal="true" resizable="false">
			<h:form>
				<div class="ui-grid ui-grid-responsive ui-grid-row ui-grid-col-6">
					<p:messages id="messages1" for="msgs1" showDetail="true" autoUpdate="true"
						closable="true" />
					<h:panelGrid columns="2" cellpadding="5">
						<h:outputLabel for="name" value="#{out.partners_table_name}"
							style="font-weight:bold" />
						<p:inputText id="name"
							value="#{partnerController.selectedPartner.name}" />
						<h:outputLabel for="address" value="#{out.partners_table_address}"
							style="font-weight:bold" />
						<p:inputText id="address"
							value="#{partnerController.selectedPartner.address}">
						</p:inputText>
						<h:outputLabel for="type" value="#{out.partners_table_type}"
							style="font-weight:bold" />
						<p:selectOneListbox id="type"
							value="#{partnerController.selectedPartner.type}">
							<f:selectItem itemLabel="normal" itemValue="normal" />
							<f:selectItem itemLabel="premium" itemValue="premium" />
						</p:selectOneListbox>
					</h:panelGrid>
					<h:panelGrid columns="2">
						<p:commandButton value="#{out.profile_save}"
							update=":partnerForm:partnerTable" icon="ui-icon-eject"
							actionListener="#{partnerController.updatePartner}" />
						<p:commandButton update=":selectedPartnerForm" ajax="false"
							validateClient="false" immediate="true"
							action="#{partnerController.unselectPartner()}"
							icon="ui-icon-close" title="Cancel" value="#{out.login_cancel}"></p:commandButton>
					</h:panelGrid>
				</div>
			</h:form>
		</p:dialog>

		<p:dialog id="createPartnerDialog"
			header="#{out.partners_create_partner}" widgetVar="dlg_create"
			modal="true" resizable="false">
			<h:form>
				<div class="ui-grid ui-grid-responsive ui-grid-row ui-grid-col-6">
					<p:messages id="messages2" for="msgs2" showDetail="true" autoUpdate="true"
						closable="true" />
					<h:panelGrid id="inputData" columns="2" cellpadding="5">
						<h:outputLabel for="name" value="#{out.partners_table_name}"
							style="font-weight:bold" />
						<p:inputText id="name" value="#{partnerController.newPartnerName}" />
						<h:outputLabel for="address" value="#{out.partners_table_address}"
							style="font-weight:bold" />
						<p:inputText id="address"
							value="#{partnerController.newPartnerAddress}">
						</p:inputText>
						<h:outputLabel for="tp" value="#{out.partners_table_type}"
							style="font-weight:bold" />
						<p:selectOneListbox id="tp"
							value="#{partnerController.newPartnerType}">
							<f:selectItem itemLabel="normal" itemValue="normal" />
							<f:selectItem itemLabel="premium" itemValue="premium" />
						</p:selectOneListbox>
					</h:panelGrid>
					<h:panelGrid columns="2" cellpadding="5">
						<p:commandButton value="#{out.profile_save}"
							update=":partnerForm:partnerTable" icon="ui-icon-eject"
							actionListener="#{partnerController.addPartner}" />
						<p:commandButton update=":selectedPartnerForm" ajax="false"
							validateClient="false" immediate="true"
							action="#{partnerController.unselectPartner()}"
							icon="ui-icon-close" title="Cancel" value="#{out.login_cancel}"></p:commandButton>
					</h:panelGrid>
				</div>
			</h:form>
		</p:dialog>

		<p:dialog id="showOrdersDialog" header="#{out.orders_of_partner}"
			widgetVar="dlg_orders" modal="true" resizable="false">
			<h:form>
				<p:dataTable var="order"
					value="#{partnerController.selectedPartnersOrders}"
					paginator="true" rows="10"
					paginatorTemplate="{RowsPerPageDropdown} {FirstPageLink} {PreviousPageLink} {CurrentPageReport} {NextPageLink} {LastPageLink}"
					rowsPerPageTemplate="5,10,15" id="ordersTable"
                                        emptyMessage="#{out.empty_message}">
					<p:column style="width:3%">
						<p:rowToggler />
					</p:column>
					<p:column headerText="#{out.orders_form_date}"
						sortBy="#{order.date}" filterBy="#{order.date}">
						<h:outputText value="#{order.date}" />
					</p:column>
					<p:column headerText="#{out.orders_form_status}"
						sortBy="#{order.status}" filterBy="#{order.status}">
						<h:outputText value="#{partnerController.getOrderStatus(order.status)}" />
					</p:column>

					<p:rowExpansion>
						<p:dataTable
							value="#{partnerController.manageRegistryFacadeService.findByOrder(order)}"
							var="registry"
                                                        emptyMessage="#{out.empty_message}">
							<p:column headerText="#{out.orders_details_beer_name}">
								<h:outputText value="#{registry.beer.name}" />
							</p:column>
							<p:column headerText="#{out.worker_order_quantity}">
								<h:outputText value="#{registry.quantity}" />
							</p:column>
						</p:dataTable>
					</p:rowExpansion>
				</p:dataTable>
			</h:form>
		</p:dialog>

		<h:form id="partnerCommandsForm">
			<h:panelGrid columns="3" cellpadding="5">
				<p:commandButton icon="ui-icon-arrowrefresh-1-n"
					value="#{out.partners_table_EditButton}" type="button"
					onclick="PF('dlg_edit').show()"
					disabled="#{partnerController.selectedPartner == null}">
				</p:commandButton>
				<p:commandButton icon="ui-icon-arrowrefresh-1-n"
					value="#{out.partners_newPartner_addButton}" type="button"
					onclick="PF('dlg_create').show()">
				</p:commandButton>
				<p:commandButton value="#{out.partners_showOrders}" type="button"
					disabled="#{partnerController.selectedPartner == null}"
					onclick="PF('dlg_orders').show()">
				</p:commandButton>
			</h:panelGrid>
		</h:form>

	</ui:define>

</ui:composition>